import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/input_item_sms_code.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/update_pay_passowrd_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class UpdatePayPassowrdPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    /// type == 1 时，标题为设置支付密码
    int type = 0;
    if (Get.arguments != null) {
      Map<String, dynamic> argumentsMap = Get.arguments;
      type = argumentsMap['type'];
    }    
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: type == 1 ? '设置支付密码' : '修改支付密码',
        backgroundColor: pageBgColor,
      ),
      body: GetBuilder<UpdatePayPassowrdPageController>(
        init: UpdatePayPassowrdPageController(),
        builder: (controller) {
          return ScrollConfigurationCommon(child: ListView(
            children: [
              Obx(() => InputItem(
                title: '新密码', 
                hintText: '请输入六位数字密码',
                defaultValue: controller.password.value,
                showErrorText: !controller.checkPassword.value,
                errorText: '六位数字密码',
                maxLength: 6,
                obscureText: true,
                inputFormatters: [
                  FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                ],
                onChenge: (String value) {
                  controller.password.value = value;
                  controller.checkUpdatePassword();
                }
              )),
              Obx(() => InputItem(
                title: '确认密码', 
                hintText: '请确认新密码',
                defaultValue: controller.confirmPassword.value,
                showErrorText: !controller.checkConfirmPassword.value,
                errorText: '密码不一致',
                maxLength: 6,
                obscureText: true,
                inputFormatters: [
                  FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                ],
                onChenge: (String value) {
                  controller.confirmPassword.value = value;
                  controller.checkConfirmPasswordFun();
                }
              )),
              Obx(() => InputItemSmsCode(
                duration: controller.duration.value,
                value: controller.verificationCode.value,
                showError: !controller.checkVerificationCode.value,
                onChenge: (String value) {
                  controller.verificationCode.value = value;
                  controller.checkVerificationCodeFun();
                }, 
                onTap: () {
                  if (controller.duration.value >= 60) {
                    controller.getSmsCode();
                  }
                }, 
              )),
              Container(
                margin: EdgeInsets.only(top: 54.h),
                padding: EdgeInsets.symmetric(horizontal: 25.w),
                child: ButtonUtil(
                  text: '保存', 
                  fontSize: 16.w, 
                  borderRadius: 30.w, 
                  height: 36.h, 
                  onTop: () {
                    controller.editPassword();
                  }
                ),
              )
            ],
          ));
        },
      )
    );
  }
}